package com.foreverwin.mes.production.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.foreverwin.mes.common.enums.HandleEnum;
import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.exception.BusinessException;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.core.utils.DateUtil;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.production.mapper.MaterialRequisitionLogMapper;
import com.foreverwin.mes.production.model.MaterialRequisitionLog;
import com.foreverwin.mes.production.model.MaterialRequisitionSummary;
import com.foreverwin.mes.production.service.MaterialRequisitionLogService;
import com.foreverwin.mes.production.service.MaterialRequisitionSummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 领料记录 服务实现类
 * </p>
 *
 * @author Ervin Chen
 * @since 2020-02-24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MaterialRequisitionLogServiceImpl extends ServiceImpl<MaterialRequisitionLogMapper, MaterialRequisitionLog> implements MaterialRequisitionLogService {


    @Autowired
    private MaterialRequisitionLogMapper materialRequisitionLogMapper;
    @Autowired
    private MaterialRequisitionSummaryService requisitionSummaryService;

    @Override
    public Page<MaterialRequisitionLog> selectPage(FrontPage<MaterialRequisitionLog> frontPage, MaterialRequisitionLog materialRequisitionLog) {
        EntityWrapper<MaterialRequisitionLog> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(materialRequisitionLog);
        return super.selectPage(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<MaterialRequisitionLog> selectList(MaterialRequisitionLog materialRequisitionLog) {
        EntityWrapper<MaterialRequisitionLog> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(materialRequisitionLog);
        return super.selectList(queryWrapper);
    }

    @Override
    public boolean insertOrUpdate(List<MaterialRequisitionLog> materialRequisitionLogs) {
        String site = CommonMethods.getSite();
        String userBo = CommonMethods.getUserBo();

        materialRequisitionLogs.forEach(materialRequisitionLog -> {
            String shopOrder = materialRequisitionLog.getShopOrder();
            String taskNo = materialRequisitionLog.getTaskNo();
            String itemBo = materialRequisitionLog.getItemBo();
            String unitOfMeasure = materialRequisitionLog.getUnitOfMeasure();
            Double taskQty = materialRequisitionLog.getTaskQty();
            Double pickQty = materialRequisitionLog.getPickQty();
            Date now = new Date();

            //校验字段
            validateFields(materialRequisitionLog);

            //领料数量汇总
            String requisitionSummaryBo = HandleEnum.MATERIAL_REQUISITION_SUMMARY.getHandle(site, shopOrder, taskNo, itemBo);
            MaterialRequisitionSummary requisitionSummary = requisitionSummaryService.selectById(requisitionSummaryBo);
            if (requisitionSummary != null) {
                requisitionSummary.setReceivedQty(requisitionSummary.getReceivedQty() + pickQty);
                requisitionSummary.setUpdateUserBo(userBo);
                requisitionSummary.setUpdatedDateTime(now);
                requisitionSummaryService.updateById(requisitionSummary);
            } else {
                requisitionSummary = new MaterialRequisitionSummary();
                requisitionSummary.setHandle(requisitionSummaryBo);
                requisitionSummary.setSite(site);
                requisitionSummary.setShopOrder(shopOrder);
                requisitionSummary.setTaskNo(taskNo);
                requisitionSummary.setItemBo(itemBo);
                requisitionSummary.setTaskQty(taskQty);
                requisitionSummary.setReceivedQty(pickQty);
                requisitionSummary.setUnitOfMeasure(unitOfMeasure);
                requisitionSummary.setCreateUserBo(userBo);
                requisitionSummary.setCreatedDateTime(now);
                requisitionSummaryService.insert(requisitionSummary);
            }

            //保存领料记录
            materialRequisitionLog.setHandle(HandleEnum.MATERIAL_REQUISITION_LOG.getHandle(site, shopOrder, taskNo, itemBo, DateUtil.format(now)));
            materialRequisitionLog.setSite(site);
            materialRequisitionLog.setCreateUserBo(userBo);
            materialRequisitionLog.setCreatedDateTime(now);
            insert(materialRequisitionLog);
        });

        //打印领料单
        printMaterialRequisition();

        return true;
    }

    private boolean validateFields(MaterialRequisitionLog materialRequisitionLog) {
        String shopOrder = materialRequisitionLog.getShopOrder();
        String taskNo = materialRequisitionLog.getTaskNo();
        String itemBo = materialRequisitionLog.getItemBo();
        Double pickQty = materialRequisitionLog.getPickQty();

        if (StringUtils.isBlank(shopOrder)) {
            throw BusinessException.build("production.materialRequisition.shopOrderNotBlank", new Object[]{});
        }
        if (StringUtils.isBlank(taskNo)) {
            throw BusinessException.build("production.materialRequisition.taskNoNotBlank", new Object[]{});
        }
        if (StringUtils.isBlank(itemBo)) {
            throw BusinessException.build("production.materialRequisition.itemNotBlank", new Object[]{});
        }
        if (pickQty == null || pickQty <= 0) {
            throw BusinessException.build("production.materialRequisition.pickQtyNotBlankOrNotValid", new Object[]{});
        }
        return true;
    }

    private void printMaterialRequisition() {

    }

}